package com.utils;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class DbUtils{

    private static final String URL = "jdbc:mysql://127.0.0.1:3306/shopping";
    private static final String USER = "root";
    private static final String PASSWORD = "daliang9527_.";

    public DbUtils() {
	try {
	    Class.forName("com.mysql.jdbc.Driver");
	} catch (ClassNotFoundException e) {
	    e.printStackTrace();
	    System.out.println("驱动加载失败" + e.getMessage());
	}
    }

    // 连接
    public Connection getConnect() {
	try {
	    return DriverManager.getConnection(URL, USER, PASSWORD);
	} catch (SQLException e) {
	    e.printStackTrace();
	    return null;
	}
    }

    // 增删改通用方法
    public boolean executeUpdate(String sql, Object...object) {
	boolean flag=false;
	PreparedStatement pstmt = null;
	Connection connection = getConnect();
	try {
	    pstmt = connection.prepareStatement(sql);
	    for (int i = 0; i < object.length; i++) {
		pstmt.setObject(i + 1, object[i]);
	    }
	    if (pstmt.executeUpdate() > 0) {
		flag=true;
	    }
	    return flag;
	} catch (SQLException e) {
	    e.printStackTrace();
	    System.out.println("出错" + e.getMessage());
	} finally {
	    closePreparedStatement(pstmt);
	    closeConnection(connection);
	}
	return flag;
    }

    // 查询通用方法
    @SuppressWarnings("unchecked")
    public <T extends Object>List<T> executeQuery(String sql, Object... object) {
	PreparedStatement pstmt = null;
	ResultSet rs = null;
	List<Object> list = new ArrayList<Object>();
	Connection connection = getConnect();
	try {
	    pstmt = connection.prepareStatement(sql);
	    for (int i = 0; i < object.length; i++) {
		pstmt.setObject(i + 1, object[i]);
	    }
	    rs = pstmt.executeQuery();
	    // 列数
	    int count = rs.getMetaData().getColumnCount();
	    // 每一行取出属性名和值放置在Map中，添加到list集合中
	    while (rs.next()) {
		Map<String, Object> map = new HashMap<String, Object>();
		for (int i = 1; i <= count; i++) {
		    // rs.getMetaData().getColumnLabel(i) 获取列名
		    map.put(rs.getMetaData().getColumnLabel(i), rs.getObject(i));
		}
		list.add(map);
	    }
	} catch (SQLException e) {
	    e.printStackTrace();
	} finally {
	    closeResultSet(rs);
	    closePreparedStatement(pstmt);
	    closeConnection(connection);
	}
	return (List<T>) list;
    }

    // 关闭连接
    public void closeConnection(Connection connection) {
	if (connection != null) {
	    try {
		connection.close();
	    } catch (SQLException e) {
		e.printStackTrace();
	    }
	    connection = null;
	}
    }

    // 关闭PreparedStatement
    public void closePreparedStatement(PreparedStatement pstmt) {
	if (pstmt != null) {
	    try {
		pstmt.close();
	    } catch (SQLException e) {
		e.printStackTrace();
	    }
	    pstmt = null;
	}
    }

    // 关闭ResultSet
    public void closeResultSet(ResultSet resultSet) {
	if (resultSet != null) {
	    try {
		resultSet.close();
	    } catch (SQLException e) {
		e.printStackTrace();
	    }
	    resultSet = null;
	}
    }
}
